<template>
  <div class="lottery_container">
    <div class="lottery-wrapper">
      <div class="title">
        <img
          src="//lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/title.b704296.png"
        />
        <p>抽大奖</p>
      </div>
      <div class="main">
        <p class="title">幸运抽奖</p>
        <div class="box">
          <div class="left_part">
            <div class="header">
              <p>当前矿石数：{{ total_diamond }}</p>
            </div>
            <div class="lottery">
              <div class="turntable-box">
                <div class="upper-border up-down">
                  <div class="dot vertex"></div>
                  <div class="dot border left-border-dot"></div>
                  <div class="dot white"></div>
                  <div class="dot border right-border-dot"></div>
                  <div class="dot vertex"></div>
                </div>
                <div class="lower-border up-down">
                  <div class="dot vertex"></div>
                  <div class="dot border left-border-dot"></div>
                  <div class="dot white"></div>
                  <div class="dot border right-border-dot"></div>
                  <div class="dot vertex"></div>
                </div>
                <div class="left-border left-right">
                  <div class="dot vertex"></div>
                  <div class="dot border left-border-dot"></div>
                  <div class="dot white"></div>
                  <div class="dot border right-border-dot"></div>
                  <div class="dot vertex"></div>
                </div>
                <div class="right-border left-right">
                  <div class="dot vertex"></div>
                  <div class="dot border left-border-dot"></div>
                  <div class="dot white"></div>
                  <div class="dot border right-border-dot"></div>
                  <div class="dot vertex"></div>
                </div>
                <div class="blocks">
                  <div class="item-container">
                    <div
                      class="turntable-item item"
                      v-for="(item, index) in lotteryList"
                      :key="index"
                      :class="{ lottery: index === 4, chosen: index == chose }"
                      @click="runLottery(index)"
                    >
                      <div class="image" v-if="index !== 4">
                        <img :src="item.lottery_image" alt="image" />
                      </div>
                      <div class="lottery-text" v-else>抽奖</div>
                      <div class="text">
                        {{ index === 4 ? `${cost}矿石/次` : item.lottery_name }}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="right_part">
            <p class="title">中奖列表</p>
            <div class="winninglist_container winning_list">
              <div class="content">
                <div class="item" v-for="(item,index) in giftsList" :key="index">
                  <p class="message">🎉 恭喜 <span class="username">您</span> 抽中{{item.name}}</p>
                  <p class="date">{{item.date}}</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getLotteryConfig, getLotteryRes } from "../assets/utils/services/api";
import moment from "moment";
export default {
  data() {
    return {
      lotteryList: [], // 奖品列表
      map: [0, 3, 6, 7, 8, 5, 2, 1], // 跑马灯顺序
      cost: null, //每次抽奖消耗的钻石数
      total_diamond: 1000, // 钻石数量
      chose: -1, // 当前选中奖品
      times: 5, // 转几圈
      speed: 50, // 速度
      setIntervalinit: null, // 定时器
      lockBtn: false,
      giftsList: [],
    };
  },
  async created() {
    const lotteryConfigRes = await getLotteryConfig();
    if (lotteryConfigRes.code == 0) {
      lotteryConfigRes.data.lottery.forEach((lottery, index) => {
        if (index == 4) {
          this.lotteryList.push({});
        }
        this.lotteryList.push(lottery);
      });
      this.cost = lotteryConfigRes.data.point_cost;
    }
    const storageDiamond = window.localStorage.getItem('diamond');
    if(storageDiamond){
      if(storageDiamond) {
        this.total_diamond = parseInt(storageDiamond);
      }
    }else{
      if(lotteryConfigRes.code == 0){
        this.total_diamond = lotteryConfigRes.data.diamond
      }
    }
    const storageGiftsList = window.localStorage.getItem('giftsList');
    if(storageGiftsList){
      if (storageGiftsList) {
        this.$set(this, 'giftsList', JSON.parse(storageGiftsList));
      }
    }
  },

  methods: {
    async runLottery(e) {
      if (this.lockBtn) {
        console.log("抽奖中，无法点击，请等待抽奖完成");
        return false;
      }
      if(this.total_diamond < this.cost) {
        alert("您的余额不足");
        return false;
      }
      if (e != 4) {
        return false;
      } else {
        this.lockBtn = true;
        const lotteryRes = await getLotteryRes();
        if (lotteryRes.code == 0) {
          let resKey = 0;
          this.lotteryList.forEach((item, index) => {
            if (item.lottery_id == lotteryRes.data.lottery_id) {
              resKey = index;
            }
          });
          this.total_diamond -= this.cost;
          window.localStorage.setItem('diamond', this.total_diamond);
          this.execLottery(this.times, this.speed, resKey);
        }
      }
    },
    
    execLottery(times, speed, resKey) {
      this.setIntervalinit = setInterval(() => {
        // this.chose = this.map[Math.floor(Math.random()*this.map.length)]; // 随机
        let next = this.map.indexOf(this.chose) + 1;
        if (times > 0) {
          if (this.map.length == next) {
            // 起点
            times--;
            this.chose = 0;
            clearInterval(this.setIntervalinit);
            speed += 20;
            this.execLottery(times, speed, resKey);
          } else {
            // 按照路线选择
            this.chose = this.map[next];
          }
        } else {
          // 最后一圈了
          if (this.chose == resKey) {
            clearInterval(this.setIntervalinit);
            this.lockBtn = false;
            const temp = {
              name: this.lotteryList[resKey].lottery_name,
              date: moment(new Date().getTime()).format("YYYY-MM-DD HH:mm:ss")
            };
            this.giftsList.push(temp);
            const storageGiftsList = window.localStorage.getItem('giftsList');
            if (storageGiftsList) {
              const list = JSON.parse(storageGiftsList);
              list.push(temp);
              window.localStorage.setItem('giftsList', JSON.stringify(list));
            }else{
              window.localStorage.setItem('giftsList', JSON.stringify([temp]));
            }
            alert(`恭喜您，抽中了：${this.lotteryList[resKey].lottery_name}`);
            this.chose = -1;
            return true;
          } else {
            this.chose = this.map[next];
            clearInterval(this.setIntervalinit);
            speed += 100;
            this.execLottery(times, speed, resKey);
          }
        }
      }, speed);
    },
  },
};
</script>

<style lang="scss" scoped>
.lottery_container {
  .lottery-wrapper {
    width: 946px;
    margin: 0 auto;
    min-height: inherit;
    position: relative;
    background: url(https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/img/background.f2441ca.png)
      no-repeat;
    background-size: 100% 100%;
    border-radius: 2px;
    overflow: hidden;
    .title {
      margin-top: 82px;
      & > img {
        display: block;
        margin-left: 254px;
        width: 500px;
      }
      & > p {
        font-size: 18px;
        line-height: 24px;
        text-align: center;
        color: #e8f3ff;
        text-shadow: 0 2px 2px rgba(0, 0, 0, 0.24);
        margin: 24px 0 0;
      }
    }
    .main {
      position: relative;
      z-index: 9;
      padding: 40px 32px 40px;
      .title {
        position: relative;
        padding-left: 20px;
        font-size: 20px;
        line-height: 28px;
        color: #fadd95;
        margin: 0;
        &::before {
          content: "";
          position: absolute;
          left: 0;
          top: 0;
          bottom: 0;
          margin: auto 0;
          width: 8px;
          height: 20px;
          display: block;
          border-radius: 50px;
          background: #e37815;
        }
      }
      .box {
        display: flex;
        flex-direction: row;
        margin-top: 16px;
        .left_part {
          width: 486px;
          flex-shrink: 0;
          margin-right: 32px;
          .header {
            margin-bottom: 16px;
            & > p {
              font-size: 16px;
              line-height: 24px;
              color: #fff;
              margin: 0;
            }
          }
          .lottery {
            width: 486px;
            height: 396px;
            .turntable-box {
              background: #fadd95;
              box-shadow: inset 0 0 16px #ff9a2e;
              border-radius: 12px;
              box-sizing: border-box;
              position: relative;
              padding: 24px;
              width: 100%;
              height: 100%;
              .up-down {
                position: absolute;
                left: 8px;
                display: flex;
                align-items: center;
                height: 24px;
                width: calc(100% - 16px);
                justify-content: space-between;
              }
              .left-right {
                position: absolute;
                top: 8px;
                display: flex;
                align-items: center;
                width: 24px;
                height: calc(100% - 16px);
                justify-content: space-between;
                flex-direction: column;
              }
              .upper-border {
                top: 0;
              }
              .lower-border {
                bottom: 0;
              }
              .left-border {
                left: 0;
              }
              .right-border {
                right: 0;
              }
              .vertex {
                background: #e37815;
              }
              .dot {
                border-radius: 50%;
                box-sizing: border-box;
                width: 12px;
                height: 12px;
              }

              .blocks {
                width: 100%;
                height: 100%;
                .item-container {
                  width: 100%;
                  height: 100%;
                  background: #e37815;
                  border-radius: 8px;
                  padding: 16px;
                  box-sizing: border-box;
                  display: flex;
                  justify-content: space-between;
                  flex-wrap: wrap;
                  flex-direction: column;
                  align-content: space-between;
                  .turntable-item {
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    justify-content: center;
                    background: #fdf3f3;
                    border-radius: 4px;
                    position: relative;
                    -webkit-user-select: none;
                    -moz-user-select: none;
                    -ms-user-select: none;
                    user-select: none;
                    .image {
                      height: 50px;
                      width: 72px;
                      display: flex;
                      justify-content: center;
                      align-items: center;
                      & > img {
                        max-width: 40px;
                        max-height: 40px;
                        height: 100%;
                      }
                    }
                    .lottery-text {
                      font-size: 32px;
                      line-height: 42px;
                      font-weight: 600;
                      color: #a74b00;
                    }
                  }
                  .lottery {
                    background: #ffe6a6;
                    box-shadow: inset 0 0 16px #ffa800;
                    cursor: pointer;
                  }
                  .item {
                    width: calc(33.33333% - 5.33333px);
                    height: calc(33.33333% - 5.33333px);
                  }
                  .chosen {
                    background: #ffcf8b;
                  }
                }
              }
            }
          }
        }
        .right_part {
          width: 364px;
          flex-shrink: 0;
          .title {
            position: relative;
            padding-left: 20px;
            font-size: 20px;
            line-height: 28px;
            color: #fadd95;
            margin: 0;
            &::before{
              content: "";
              position: absolute;
              left: 0;
              top: 0;
              bottom: 0;
              margin: auto 0;
              width: 8px;
              height: 20px;
              display: block;
              border-radius: 50px;
              background: #e37815;
            }
          }
          .winninglist_container {
            overflow-y: scroll;
            &::-webkit-scrollbar {
              width: 1px;
            }
            .item {
              padding-bottom: 8px;
              display: flex;
              font-size: 14px;
              line-height: 22px;
              color: #fff;
              .message {
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
                word-break: break-all;
                flex: 1;
                padding-right: 5px;
                .username{
                  color: #fadd95;
                }
              }
              .date{
                color: #e8f3ff;
              }
            }
          }
          .winning_list {
            width: 100%;
            height: 172px;
            margin-top: 20px;
            margin-bottom: 24px;
            border-radius: 12px;
            background: rgba(255,247,232,.3);
            padding: 16px;
            box-sizing: border-box;
          }
        }
      }
    }
  }
}
</style>